<?php
/*
	分销中心
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
//$this->model('member')->set_parent(90,1);
$this->model('member')->min_agent_level();
$this->model('member')->upbao_agent();
$this->session('footernav','');
$return['tcommission']=$this->tcommission;
if($_RQ['op']=='main'){
	$return['agentsetting']=$this->agentsetting;
	$return['setting']=$this->plugin_setting('page_setting');
	$diytitle=array('tagent','tshop','tmyshop','tcenter','tbecome','balance_button','tcommission','tcommission1','commission_total','commission_1','balance_total','balance_1','commission_1_1','commission_0','balance_2','balance_3','balance_pay','tcommission_detail','torder','tdown','tmydown','area_title','commission_area');
	foreach($diytitle as $v){
		$return['setting'][$v]=$return['setting'][$v.'2']?:$return['setting'][$v];
	}
	if($return['pluginset']['levefromid']){
		$level=$this->getcolumn('agent_level',array('id'=>$return['pluginset']['levefromid']),'level');
		$parameter=array(
				'params'=>array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'level <'=>$level),
				'orderby'=>'level desc',
		);
		$levels=$this->getall('agent_level',$parameter['params'],'','',$parameter['orderby']);
		if($member['level_id']==intval($levels[0]['id'])){
			$return['showlevefrom']=true;
		}
	}
	$return['xieyi']=$this->plugin_setting('xieyi');
	$tagent=$this->getcolumn('agent_level',array('id'=>$return['pluginset']['levefromid']),'title');
	$tagent=$tagent?:'上级';
	$return['tagent']=$tagent;
	set_html('title',$return['setting']['title']?:'分销中心');
	if($_SESSION['onemerchid']){
		$basic=model('jy_merch/member')->getdetail($_SESSION['onemerchid'],$_SESSION['uid']);
		$basic2=model('core/member')->detail($_SESSION['uid'],array('avatar','nickname'));
		$basic=array_merge($basic2,$basic);
	}else{
		$basic=model('core/member')->basic($_SESSION['uid']);
	}
	$member=array_merge($member,$basic);
	if($member['fromuser']){
		$member['agent']=model('core/member')->basic($member['fromuser']);
	}else{
		$member['agent']=array('nickname'=>'总店');
	}
	if($member['level_id']){
		$member['level']=$this->getcolumn('agent_level',array('id'=>$member['level_id']),'title');
	}else{
		$member['level']=$return['setting']['demolevel'];
	}
	$return['ordernum']=0;
	$return['feenum']=0;
	$return['feenum'] +=$this->count('commission',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid'],'level'=>18));
	$return['feenum'] +=$this->count('commission',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid'],'level'=>0));
	$mids=array($_SESSION['uid']);
	for($i=0;$i<$this->agentsetting['level'];$i++){
		$level=$i+1;
		$return['feenum'] +=$this->count('commission',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid'],'level'=>$level));
		if($i>0){
			$mids=$this->getall('member',array('merchid'=>$_SESSION['onemerchid'],'fromuser'=>$mids),array('mid'),'mid');
		}
		if($mids){
			$mids=array_keys($mids);
			$return['ordernum'] +=$this->count('agent_order',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$mids));
		}else{
			$mids=-1;
		}
	}
	$return['member']=$member;
	$data=array();
	//已结算佣金
	$data['balance_total']=$this->sum('balance',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid']),'money');
	$data['balance_num']=$this->count('balance',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid']));
	//待结算佣金
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1,'type'=>array(0,1,3,4));
	$data['commission_1']=$this->sum('commission',$params,'money');
	$data['commission_1num']=$this->count('commission',$params,'id');
	$data['commission_total']=$this->sum('commission',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'type'=>array(0,1,3,4,20)),'money');
	$data['commission_num']=$this->count('commission',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'type'=>array(0,1,3,4)),'id');
	$data['commission_area']=$this->sum('commission',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'type'=>array(0,1,3,4)),'money');
	$levels=array();
	$fromusers=$_SESSION['uid'];
	$allagentnum=0;
	$allmember_num=0;
	for($i=0;$i<18;$i++){
		$level=$i+1;
		if(!$fromusers){
			$levels[$level]=0;
			continue;
		}
		$fromusers=$this->getall('member',array('merchid'=>$_SESSION['onemerchid'],'fromuser'=>$fromusers),array('mid'),'mid');
		if(!$fromusers){
			$levels[$level]=0;
		}else{
			$fromusers=array_keys($fromusers);
			$levels[$level]=count($fromusers);
			$allmember_num+=$levels[$level];
			$member_ids=$this->getall('member',array('merchid'=>$_SESSION['onemerchid'],'fromuser'=>$fromusers,'is_agent'=>1),array('mid'),'mid');
			$allagentnum+=count($member_ids);
		}
	}
	$return['game_wait']=model('jy_weishop/task')->pop_game('',8);
	$agent_update=array('agent_num'=>$allagentnum,'member_num'=>$allmember_num);
	$this->update('member',$agent_update,array('mid'=>$_SESSION['uid']));
	$return['member']['member_num']=$allmember_num;
	$return['data']=$data;
	$return['xuniname']=$this->xuniname;
	$return['rank']=$this->plugin_setting('rank');
	$htmls=array(
		array('type'=>'data','num'=>12,'one_num'=>3,'title'=>'总额/数量','datas'=>array(
			array('title'=>'总佣金','num'=>2,'data'=>array($data['commission_total'],$data['commission_num'])),
			array('title'=>'已结算','yuan'=>1,'num'=>2,'data'=>array($data['balance_total'],$data['balance_num'])),
			array('title'=>'待结算','yuan'=>1,'num'=>2,'data'=>array($data['commission_1'],$data['commission_1num'])),
			array('title'=>'团队人数','num'=>2,'data'=>array($allmember_num,$allmember_num)),
		)),
	);
	if($_SESSION['os']=='windows'){
		$return['htmls']=$htmls;
	}
	exi($return);
}
if($_RQ['op']=='apply'){
	$apply=$this->get('agent_apply',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid']));
	if($apply['status']==0 && $apply['id']){
		exi('您已申请，请等待审核！','error');
	}
		$post=get_postdata();
		if(!$post['agree']){
			exi('请先阅读协议！','error');
		}
		if(!$post['realname']){
			exi('请输入姓名！','error');
		}
		if(!$post['mobile']){
			exi('请输入联系电话！','error');
		}
		if($post['diyformfields']){
			foreach ($post['diyformfields'] as $item) {
				if($item['ismust']==1 && empty($item['data'])){
					exi('请完善'.$item['title'].'内容后再提交','error');
				}
			}
		}
		$member_update=array(
			'realname'=>trim($post['realname']),
			'mobile'=>trim($post['mobile']),
			'diyformdata'=>ijson_encode($post['diyformfields']),
		);
		if($post['wechat']){
			$member_update['wechat']=trim($post['wechat']);
		}
		$this->model('member')->member_update($_SESSION['uid'],$member_update);	
		if($post['fromuser']){
			$fromuser=intval($post['fromuser']);
			if($fromuser){
				$this->model('member')->set_parent($_SESSION['uid'],$fromuser);
			}
		}
		$this->model('member')->apply_agent($_SESSION['uid']);
		exi('申请成功！请耐心等待管理员审核！','',custom_url('jy_weishop/index/',true));
}
if($_RQ['op']=='congzhi'){
	$post=get_postdata();
	if($post['isbaofee']){
		$member=pdo_get('jy_mjicai_member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid']));
	}else{
		if($_SESSION['onemerchid']){
			$member=model('jy_merch/member')->getdetail($_SESSION['onemerchid'],$_SESSION['uid']);
		}else{
			$member=pdo_get('core_members',array('id'=>$_SESSION['uid']));
		}
	}
	if(!$post['fee']){
		exi('请输入转账'.$this->db_title.'！','erroe');
	}
	if(!$post['mobile']){
		exi('请输入收款人账户/手机号！','erroe');
	}
	if($post['isbaofee']){
		if($member['baofee']<$post['fee']){
			exi('转账额度不能大于，当前剩余'.$this->db_title.'！','erroe');
		}
	}else{
		if($member['credit1']<$post['fee']){
			exi('转账额度不能大于，当前积分额度！','erroe');
		}
	}
	$tomember=pdo_get('core_members',array('username'=>$post['mobile']));
	$tomid=$tomember['id'];
	if(!$tomid){
		$tomember=pdo_get('core_members',array('mobile'=>$post['mobile']));
		$tomid=$tomember['onemid']?:$tomember['id'];
	}
	if(!$tomid){
		exi('没找到收款人，转账失败！','erroe');
	}
	$team=pdo_get('jy_mjicai_team',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$tomid,'uniacid'=>$_SESSION['uniacid']));
	if(!$team['status']){
		exi('收款人不是团队长门店，转账失败！','erroe');
	}
	if($post['isbaofee']){
		pdo_update('jy_mjicai_member',array('baofee -='=>$post['fee']),array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		pdo_update('jy_mjicai_member',array('baofee +='=>$post['fee']),array('mid'=>$tomid,'uniacid'=>$_SESSION['uniacid']));
		$feelog=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$_SESSION['uid'],
						'fee'=>$post['fee'],
						'remark'=>'转账给:'.$post['mobile'],
		);
		$this->model('model')->add_feelog($feelog,10);
		$feelog['mid']=$tomid;
		$this->model('model')->add_feelog($feelog,11);
	}else{
		if($_SESSION['onemerchid']){
			model('jy_merch/member')->member_credit($_SESSION['onemerchid'],$_SESSION['uid'],'credit1',$post['fee'],0,'jy_mjicai','转账给:'.$post['mobile']);
			model('jy_merch/member')->member_credit($_SESSION['onemerchid'],$tomid,'credit1',$post['fee'],1,'jy_mjicai','转账给:'.$member['username'].'的转账');
		}else{
			model('core/member')->cut_credit1($_SESSION['uid'],$post['fee'],'转账给:'.$post['mobile']);
			model('core/member')->add_credit1($tomid,$post['fee'],$member['username'].'的转账');
		}
	}
	exi('转账成功！');
}
if($_RQ['op']=='getlist'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'type'=>9);
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice('jy_mjicai_feelog',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	foreach($return['list'] as &$v){
			$teampaixu=pdo_getcolumn('jy_mjicai_cang',array('id'=>$v['cangid'],'uniacid'=>$_SESSION['uniacid']),'paixu');
			$yaopaixu=pdo_getcolumn('jy_mjicai_yao',array('id'=>$v['cangid'],'uniacid'=>$_SESSION['uniacid']),'paixu');
			$v['createtime']=date('Y-m-d H:m:s',$v['createtime']);
			$v['statustitle']=$this->canlog_status[$v['status']];
			$v['typetitle']=$this->feelog_type[$v['type']];
			$v['db_title']=$this->db_title;
			$v['title']='第'.$teampaixu.'仓第'.$yaopaixu.''.$this->y_title;
	}
	exi($return);
}
?>